package com.design.testdemo.view;

import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Point;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.View;
import android.view.animation.LinearInterpolator;

/**
 * @Auther: Timmy
 * @Data: 2017/6/17
 * @Fuction:
 */

public class OptionCircle extends View {
    private static final float MIDDLE_LAYER_LARGE_SCALE = 57 / 62.F;
    private static final float SMALL_CIRCLE_DEGREE_OFFSET = 0.7F;

    private float drgeePercent;

    private Paint progressBgPaint;
    private int viewSize;
    private Point circleCenterPoint;
    private float mMiddleLayerSize;
    private Paint smallCirclePaint;
    private ValueAnimator mValueAnimator;
    private Paint mTextPaint;

    public OptionCircle(Context context) {
        this(context, null);
    }

    public OptionCircle(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public OptionCircle(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);


        initData();

    }

    public int midlleLayerProgressWidth = 0;
    private static final int DEF_VIEW_SIZE = 250;

    private void initData() {
        progressBgPaint = creatPaint(Color.WHITE, 0, Paint.Style.STROKE, midlleLayerProgressWidth);
        progressBgPaint.setStrokeWidth(dp2px(1));
        smallCirclePaint = creatPaint(Color.WHITE, 0, Paint.Style.FILL, 0);


        mTextPaint = new Paint();
        mTextPaint.setColor(Color.WHITE);
        mTextPaint.setTextSize(30);
        mTextPaint.setAntiAlias(true);
        mTextPaint.setTextAlign(Paint.Align.CENTER);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        viewSize = w - h >= 0 ? h : w;
        circleCenterPoint = new Point(viewSize / 2, viewSize / 2);
        //mTextPaint = new Paint();
        mMiddleLayerSize = viewSize * MIDDLE_LAYER_LARGE_SCALE;
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);

        int widthSize;
        int heightSize;

        if (widthMode == MeasureSpec.AT_MOST || widthMode == MeasureSpec.UNSPECIFIED) {
            widthSize = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, DEF_VIEW_SIZE, getResources().getDisplayMetrics());
            widthMeasureSpec = MeasureSpec.makeMeasureSpec(widthSize, MeasureSpec.EXACTLY);
        }

        if (heightMode == MeasureSpec.AT_MOST || heightMode == MeasureSpec.UNSPECIFIED) {
            heightSize = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, DEF_VIEW_SIZE, getResources().getDisplayMetrics());
            heightMeasureSpec = MeasureSpec.makeMeasureSpec(heightSize, MeasureSpec.EXACTLY);
        }
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    private Paint creatPaint(int paintColor, int textSize, Paint.Style style, int lineWidth) {
        Paint paint = new Paint();
        paint.setColor(paintColor);
        paint.setAntiAlias(true);
        paint.setStrokeWidth(lineWidth);
        paint.setDither(true);
        paint.setTextSize(textSize);
        paint.setStyle(style);
        paint.setStrokeCap(Paint.Cap.ROUND);
        paint.setStrokeJoin(Paint.Join.ROUND);
        return paint;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        drawMiddleProgressLayer(canvas);

        drawText(canvas);
    }

    private void drawMiddleProgressLayer(Canvas canvas) {
        //外切圆的坐标计算,绘制的扇形在矩形内的外切圆,注意画笔的宽度

        canvas.drawCircle(circleCenterPoint.x, circleCenterPoint.y, mMiddleLayerSize / 2 - midlleLayerProgressWidth, progressBgPaint);
        // 注意扫过的扇形的范围(进度)要和绘制的小圆点保持一致,所以我们需要从-90度开始

        if (circleVisible) {

            float animDegree = 360 * drgeePercent + SMALL_CIRCLE_DEGREE_OFFSET;
            float xiaoyuanDegree;
            float xiaoYuanX = 0, xiaoYuanY = 0;
            int tempD = (int) animDegree;
            if (tempD >= 0 && tempD < 90) {
                // 第一象限内,sin和cons正常
                xiaoyuanDegree = animDegree;
                float hudu = (float) Math.abs(Math.PI * xiaoyuanDegree / 180);
                float sinAX = (float) Math.abs(Math.sin(hudu) * (mMiddleLayerSize / 2));
                float cosAY = (float) Math.abs(Math.cos(hudu) * (mMiddleLayerSize / 2));

                xiaoYuanX = (viewSize - 2 * midlleLayerProgressWidth - 2 * sinAX) / 2 + 2 * sinAX;
                xiaoYuanY = viewSize / 2 + midlleLayerProgressWidth - cosAY;
            } else if (tempD >= 90 && tempD < 180) {
                // 第二象限内,sin和cos互换
                xiaoyuanDegree = animDegree - 90;
                float hudu = (float) Math.abs(Math.PI * xiaoyuanDegree / 180);
                float sinAX = (float) Math.abs(Math.cos(hudu) * (mMiddleLayerSize / 2));
                float cosAY = (float) Math.abs(Math.sin(hudu) * (mMiddleLayerSize / 2));

                xiaoYuanX = (viewSize - 2 * midlleLayerProgressWidth - 2 * sinAX) / 2 + 2 * sinAX;
                xiaoYuanY = viewSize / 2 + cosAY - midlleLayerProgressWidth;

            } else if (tempD >= 180 && tempD < 270) {
                // 第三象限,sin和cos正常,但是x和y的坐标计算方法发生改变
                xiaoyuanDegree = animDegree - 180;
                float hudu = (float) Math.abs(Math.PI * xiaoyuanDegree / 180);
                float sinAX = (float) Math.abs(Math.sin(hudu) * (mMiddleLayerSize / 2 - midlleLayerProgressWidth));
                float cosAY = (float) Math.abs(Math.cos(hudu) * (mMiddleLayerSize / 2));

                xiaoYuanX = viewSize / 2 - sinAX;
                xiaoYuanY = viewSize / 2 + cosAY - midlleLayerProgressWidth;

            } else if (tempD >= 270 && tempD < 360) {
                // 第四象限内,sin和cos互换,但是x和y的坐标也发生了改变
                xiaoyuanDegree = animDegree - 270;
                float hudu = (float) Math.abs(Math.PI * xiaoyuanDegree / 180);
                float sinAX = (float) Math.abs(Math.cos(hudu) * (mMiddleLayerSize / 2 + midlleLayerProgressWidth / 4));
                float cosAY = (float) Math.abs(Math.sin(hudu) * (mMiddleLayerSize / 2 - midlleLayerProgressWidth));

                xiaoYuanX = viewSize / 2 - sinAX + midlleLayerProgressWidth;
                xiaoYuanY = viewSize / 2 - cosAY;
            }

            canvas.drawCircle(xiaoYuanX, xiaoYuanY, dp2px(10 / 2), smallCirclePaint);
        }
    }

    private void drawText(Canvas canvas) {
        canvas.drawText("点击连接IU", getWidth() / 2, getHeight() / 2, mTextPaint);
    }


    public void startCountdown() {

        if (circleVisible) {
            final ValueAnimator valA = getValA(5 * 1000);
            valA.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                @Override
                public void onAnimationUpdate(ValueAnimator animation) {
                    drgeePercent = Float.valueOf(valA.getAnimatedValue().toString());
                    invalidate();
                }
            });

            valA.start();
        }
    }

    private ValueAnimator getValA(long countdownTime) {
        mValueAnimator = ValueAnimator.ofFloat(0, 1.F);
        mValueAnimator.setDuration(countdownTime);
        mValueAnimator.setInterpolator(new LinearInterpolator());
        mValueAnimator.setRepeatCount(0);
        mValueAnimator.setRepeatCount(ValueAnimator.INFINITE);
        return mValueAnimator;
    }

    public void sopAnimation() {
        if (mValueAnimator != null) {
            mValueAnimator.cancel();
        }
    }

    private boolean circleVisible = true;

    public void setCircleVisible(boolean isVisble) {
        circleVisible = isVisble;
    }

    /**
     * dp 2 px
     *
     * @param dpVal
     */
    protected int dp2px(int dpVal) {
        return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
                dpVal, getResources().getDisplayMetrics());
    }
}